/**
 * 
 */
package model;

import Repository.Repository;
import Repository.RepositoryFactory;
import constants.SMSConstants;

/**
 * @author Sandhya R
 *
 */
public class QuoteRuleSelector implements RuleSelector {

	private Repository ruleRepository = RepositoryFactory.getRepository(SMSConstants.RULE);
	private SMSObject smsObject;
	
	public QuoteRuleSelector(SMSObject smsObject){
		this.smsObject = smsObject;		
	}
	
	public boolean processRuleRequest(){
		Rule quoteRule = getRule();
		return applyRule(quoteRule);
		
	}
	
	public Rule getRule() {
		
		if(smsObject instanceof Opportunity){
			return (Rule)ruleRepository.retrieve(SMSConstants.OPPORTUNITY, SMSConstants.QUOTE);
		}
		return new NullRule();	
		
		
	}
	
	public boolean applyRule(Rule rule){
			
		Repository repository = RepositoryFactory.getRepository(SMSConstants.OPPORTUNITY);
		if(smsObject instanceof Opportunity){
			
				Opportunity opp = (Opportunity)smsObject;
				System.out.println("Generating Quote...");
				Quote quote = new Quote();
				quote.setPrice(opp.getPrice());
				quote.setQuantity(opp.getQuantity());
				opp.setQuote(quote);
				repository.update(opp.getSmsObjId(), opp);
				System.out.println("Generated Quote...");
					
			
			
			return true;
		}
		
		return false;
	}

}
